import 'package:mall/constant/app_parameters.dart';
import 'package:mall/constant/app_strings.dart';
import 'package:mall/constant/app_urls.dart';
import 'package:mall/modules/model/category_entity.dart';
import 'package:mall/modules/model/category_title_entity.dart';
import 'package:mall/utils/net/http_util.dart';

class CategoryService {
// 获取一级分类
static Future<List<CategoryEntity>> getCategoryData() async {
  var response = await HttpUtil.instance.get(AppUrls.HOME_FIRST_CATEGORY);

  if (response[AppStrings.DATA] == null) {
    throw Exception(AppStrings.SERVER_EXCEPTION);
  }

  var responseList = response[AppStrings.DATA];
  // return CategoryList.fromJson(responseList);

  return (responseList as List).map((e) => CategoryEntity.fromJson(e)).toList();
}
  
  // 获取二级分类
static Future<List<CategoryEntity>> getSubCategoryData(int categoryId) async {
  var parameters = {AppParameters.ID: categoryId};
  var response = await HttpUtil.instance.get(AppUrls.HOME_SECOND_CATEGORY, parameters: parameters);

  if (response[AppStrings.DATA] == null) {
    throw Exception(AppStrings.SERVER_EXCEPTION);
  }

  var responseList = response[AppStrings.DATA];
  // return CategoryList.fromJson(responseList);

  return (responseList as List).map((e) => CategoryEntity.fromJson(e)).toList();
}

// 获取分类标题
static Future<CategoryTitleEntity> getCategoryTitle(int categoryId) async {
  var parameters = {AppParameters.ID: categoryId};
  var response = await HttpUtil.instance.get(AppUrls.CATEGORY_LIST, parameters: parameters);

  if (response[AppStrings.DATA] == null) {
    throw Exception(AppStrings.SERVER_EXCEPTION);
  }

  return CategoryTitleEntity.fromJson(response[AppStrings.DATA]);
}
}
